/*
// PREPARE REGRESSION ANALYSIS DATASET
*/


***************************************************************************************************
// merge securities/loan issuance panel with RHS variables
***************************************************************************************************
use ../Intermediate/shift_shares, clear
egen ss_pre_all_debt_ban_intnstyn1_mx = max(ss_pre_all_debt_banintsty_n1), by(borrower_id)
keep borrower_id ss_pre_all_debt_ban_intnstyn1_mx
duplicates drop
tempfile bim 
save `bim'

use ../Intermediate/borrowing_panel, clear
merge 1:1 borrower_id year using ../Intermediate/equity_issuance_panel
gen equitypanel = _merge == 3
drop _merge
merge 1:1 borrower_id year using ../Intermediate/shift_shares, keep(3) nogen
merge m:1 borrower_id using ../Intermediate/borrower_chars, keep(1 3) nogen
merge 1:1 borrower_id year using ../Raw/psedo/orbis_panel_borrower, keep(1 3)
gen has_orbis = _merge == 3
drop _merge
order borrower_name, after(borrower_id)
drop if year >= 2022 
merge m:1 borrower_id using `bim', keep(1 3) nogen

***************************************************************************************************



***************************************************************************************************
//create LHS variables
***************************************************************************************************

//create logs
foreach var of var borr_* {
    local stub = subinstr("`var'","borr_","",.)
    gen log_`stub' = log(1+`var')
    gen logim_`stub' = log(`var')  //intensive margin
}

gen borrow=(borr_debt!=0)
gen log_ltd = log(LTdebt)
gen log_size = log(Totassets)
gen log_lev = log_ltd-log_size
gen log_equity_public = log(1+equity_amt_public)



gen post15 = year > 2015
gen log_debt_banks = log(1+borr_debt -  borr_debt_non_bank)
egen total_non_bank = total(borr_debt_non_bank) 
egen total_bank = total(borr_debt) 
gen borr_debt_norel_all_pre =  borr_debt-borr_debt_rel_all_pre

//dollars of debt from anyone without policies 
gen log_debt_nopol = log(1+borr_debt -  borr_debt_has_coded_policy)
gen log_debt_noactivepol = log(1+borr_debt -  borr_debt_actvpol)
gen log_debt_norel_all_pre =  log(1+borr_debt_norel_all_pre)

***************************************************************************************************


***************************************************************************************************
//define geographic variables
***************************************************************************************************
tab Continent, mis
encode Continent, gen(cont)
encode Continent, gen(continent_i)

gen region = "Europe" if Continent == "Europe"
replace region = "North America" if Continent == "North America"
replace region = "Asia" if Continent == "Asia"
replace region = "AusNZ" if Continent == "Australasia"
replace region = "Other" if missing(region)
encode region, gen(region_i)

encode Country, gen(count)
encode Country, gen(country_i) 
***************************************************************************************************



***************************************************************************************************
//define other demographic variables
***************************************************************************************************
xtile decile_assets   = Totassets, nq(10)
xtile median_assets   = Totassets, nq(2)
egen mean_assets = mean(Totassets), by(borrower_id)
xtile median_assets_mean   = mean_assets, nq(2)
gen expansion = expansion_mining|expansion_infrastructure|expansion_power

//Share of Coal Activity variable
gen share = CoalSharePower
replace share = CoalFracRev if missing(share)
mdesc share
label var share "Corporate Coal Share (power or revenue)"

gen share_revfst = CoalFracRev
replace share_revfst = CoalSharePower if missing(share_revfst)

gen share_coalpower = CoalSharePower
replace share_coalpower = 0 if missing(share_coalpower)
gen share_coalrev = CoalFracRev
replace share_coalrev = 0 if missing(share_coalrev)
***************************************************************************************************



***************************************************************************************************



***************************************************************************************************
//Set up sample limitations
***************************************************************************************************

//define aggregate pre period borrowings by type
gen period_pre =  (year<=2013 & year >= 2009)

foreach period in pre {
foreach dtype in debt  {
    egen aggborr_`period'_`dtype' = sum(borr_`dtype'*period_`period'), by(borrower_id)
}
}

xtile quintile_assets = mean_assets if aggborr_pre_debt > 0 , nq(5)
replace  quintile_assets=0 if missing(quintile_assets)


***************************************************************************************************

***************************************************************************************************
//Set up cross sectional variables
***************************************************************************************************
bys borrower_id: gen fb = _n == 1
gen pos_debt = borr_debt > 0 if ~missing(borr_debt)
egen med_share = median(share) if ~missing(share)
gen highshare = share > med_share if ~missing(share)



sum ss_pre_all_debt_banintsty_n1 if aggborr_pre_debt > 0
gen ss_pre_all_debt_banintsty_n1_sd = ss_pre_all_debt_banintsty_n1/`r(sd)'


label define medassets 1 "Small Firm" 2 "Large Firm"
label values median_assets_mean medassets
label define medcoal 0 "Low Coal Share" 1 "High Coal Share", replace
label values highshare medcoal
label var ss_pre_all_debt_banintsty_n1_sd "$\text{Bank Exit Exposure}_{f,t}$"
label var highshare "High Coal Share"
label var coal_industry_mining "Mining Company"



save ../Intermediate/analysis_panel_firmyear, replace
